package com.springboot.controller.system;

import com.springboot.config.log.SpringBootRequestLog;
import com.springboot.config.controller.BaseController;
import com.springboot.config.error.MySpringBootException;
import com.springboot.returndata.dto.UserLoginDto;
import com.springboot.jwt.interfaces.JwtToken;
import com.springboot.returndata.vo.ResourceAuthMenuVo;
import com.springboot.service.system.UserLoginService;
import com.springboot.util.enums.RespErrorCode;
import com.springboot.util.http.ResponseBuilder;
import com.springboot.util.http.ResultResponse;
import com.springboot.util.img.VerifyCodeUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.util.List;

/**
 * @version 1.0
 * @ClassName UserLoginController
 * @Description 登录相关
 * @Author zyj
 * @date 2021/4/1 11:10
 **/
@Slf4j
@RestController
@RequestMapping("/auth")
@SpringBootRequestLog
@Tag(name = "登录相关")
public class UserLoginController extends BaseController {

    @Resource
    private UserLoginService userLoginService;

    @JwtToken
    @PostMapping("/login")
    @Operation(description = "用户名密码登录")
    public ResultResponse<String> login(@Valid @RequestBody UserLoginDto userLoginDto){
        try {
            String token = userLoginService.login(userLoginDto);
            return ResponseBuilder.successData(token);
        } catch (MySpringBootException e) {
            return ResponseBuilder.errorMessage(e.getErrorCode(), e.getMessage());
        } catch (Exception e) {
            log.error("用户名密码登录失败：", e);
            return ResponseBuilder.errorMessage(RespErrorCode.USER_NULL.getValue(), RespErrorCode.USER_NULL.getDescripe());
        }
    }


    @GetMapping("/loginOut")
    @Operation(description = "退出登录")
    public ResultResponse<String> loginOut(String jwt){
        try {
            userLoginService.loginOut(jwt);
            return ResponseBuilder.success();
        } catch (MySpringBootException e) {
            return ResponseBuilder.errorMessage(e.getErrorCode(), e.getMessage());
        } catch (Exception e) {
            return ResponseBuilder.errorMessage(RespErrorCode.USER_NULL.getValue(), RespErrorCode.USER_NULL.getDescripe());
        }
    }

    @JwtToken
    @GetMapping("/verification")
    @Operation(description = "验证码")
    public void verification(){

        try {
            HttpServletResponse response = super.getResponse();
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/jpeg");

            //生成随机字串
            String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
            //存入会话session
            HttpServletRequest request = super.getRequest();
            HttpSession session = request.getSession(true);
            session.setAttribute("captcha", verifyCode.toLowerCase());
            //生成图片
            int w = 200, h = 80;
            VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
        }catch ( Exception e){
            log.error("生成验证码失败", e);
        }

    }


    @PostMapping("/testToken")
    @Operation(description = "testToken")
    public ResultResponse<String> testToken(){
        return ResponseBuilder.success();
    }


    @PostMapping("/getAuthMenu")
    @Operation(description = "根据父级key获取所有下级权限")
    public ResultResponse<ResourceAuthMenuVo> getAuthMenu(String pMenuCode){
        try {
            List<ResourceAuthMenuVo> roleList = userLoginService.getAuthMenuByParent(pMenuCode);
            return ResponseBuilder.successData(roleList);
        }catch (Exception e){
            log.error("根据父级key获取所有下级权限 error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.GET_INFO_NULL.getValue(), RespErrorCode.GET_INFO_NULL.getDescripe());

    }
}
